package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.work.impl.dao.MaterialPurchaseRecordDao;
import com.jiantong.work.impl.manager.MaterialPurchaseRecordManager;
import com.jiantong.work.model.MaterialPurchaseRecord;
import com.jiantong.work.service.MaterialPurchaseRecordWriteService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Created by maowenjie on 2017/4/17.
 */
@Slf4j
@Service
@RpcProvider
public class MaterialPurchaseRecordWriteServiceImpl implements MaterialPurchaseRecordWriteService {

    private final MaterialPurchaseRecordManager materialPurchaseRecordManager;
     private final MaterialPurchaseRecordDao materialPurchaseRecordDao;
    @Autowired
    public MaterialPurchaseRecordWriteServiceImpl(MaterialPurchaseRecordManager materialPurchaseRecordManager,MaterialPurchaseRecordDao materialPurchaseRecordDao) {
        this.materialPurchaseRecordManager = materialPurchaseRecordManager;
        this.materialPurchaseRecordDao=materialPurchaseRecordDao;
    }

    @Override
    public Response<Map<String, Object>> create(MaterialPurchaseRecord materialPurchaseRecord, Integer toStatus) {
        try{
            return Response.ok(materialPurchaseRecordManager.createMaterialPurchaseRecord(materialPurchaseRecord,toStatus));
        }catch (Exception e){
            log.error("failed to create materialPurchaseRecord error :{}", Throwables.getStackTraceAsString(e));
            return Response.fail("materialPurchaseRecord.create.failed");
        }
    }

    @Override
    public Response<Map<String, Object>> update(MaterialPurchaseRecord materialPurchaseRecord, Integer toStatus) {
        try{
            return Response.ok(materialPurchaseRecordManager.updateMaterialPurchaseRecord(materialPurchaseRecord,toStatus));
        }catch (Exception e){
            log.error("failed to update materialPurchaseRecord error :{}", Throwables.getStackTraceAsString(e));
            return Response.fail("materialPurchaseRecord.update.failed");
        }
    }

    @Override
    public Boolean update(MaterialPurchaseRecord materialPurchaseRecord) {
        return materialPurchaseRecordDao.update(materialPurchaseRecord);
    }
}
